_import("aiviki.event");
_import("aiviki.util");
aiviki.ui.ListMenu = aiviki.Class.create("aiviki.ui.ListMenu");
aiviki.ui.ListMenu.prototype = 
	{
	init:function(a)
		{
		this.UIID=aiviki.util.getSN(this);
		this.touchObj=$byi(a);
		this.data =new aiviki.util.vector(2);
		aiviki.event.addEventByObj("EventName",this.touchObj,"click",this.show,this);
		},
	times:null,
	BORDER:5,
	UIID:null,
	MENUAREA:'avkMenu',
	MENUSTYLE:'avkMenuStyle',
	MENUITEMB:'avkMenuItemT',
	MENUITEMDF:'avkMenuItemDF',
	MENUITEMIX:'avkMenuItemIX',
	touchObj:null,
	menuObj:null,
	show:function()
		{
		if(this.menuObj==null)
			{
			var mo = document.createElement("div");
			aiviki.event.addEventByObj(this.UIID,mo,"mouseout",this.mouseout,this);
			mo.style.position="absolute";
			mo.className=this.MENUAREA;
			mo.flag=this.UIID;
			var moo = document.createElement("div");
			moo.className=this.MENUSTYLE;
			moo.flag=this.UIID;
			for(var i=0;i<this.data.getSize();i++)
				{
				if(this.data.get(i).ClassId ="aiviki.ui.avkMenuGroup")
					{
					var ul = document.createElement("ul");
					if(i==0)ul.className="one";
					ul.flag=this.UIID;
					var o = this.data.get(i);
					for(var c =0;c<o.getSize();c++)
						{
						oo = o.getItem(c);
						if(oo.ClassId =="aiviki.ui.avkMenuItemTitle")
							{
							var h6 = document.createElement("H6");
							h6.className=this.MENUITEMB;
							h6.flag=this.UIID;
							h6.appendChild(document.createTextNode(oo.text));
							ul.appendChild(h6);
							}else{
							var it = document.createElement("li");
							it.flag=this.UIID;
							var link = document.createElement("a");
							link.setAttribute("href",oo.url);
							link.flag=this.UIID;
							if(oo.target)
								link.setAttribute("target","_blank");
							link.className=this.MENUITEMDF;
							link.appendChild(document.createTextNode(oo.text));
							it.appendChild(link);
							aiviki.event.addEventByObj(this.UIID,it,"mouseover",this.itemMouseOver,this);
							aiviki.event.addEventByObj(this.UIID,it,"mouseout",this.itemMouseOut,this);


							ul.appendChild(it);
							}
						}
					moo.appendChild(ul);
					}
				}
			mo.appendChild(moo);
			document.body.appendChild(mo);
			this.menuObj=mo;
			}
		var x =aiviki.util.getX(this.touchObj)- (this.menuObj.offsetWidth-this.touchObj.offsetWidth);
		var y =aiviki.util.getEY(this.touchObj) + this.BORDER;
		this.menuObj.style.left=x+'px';
		this.menuObj.style.top=y+'px';

		},
	hide:function()
		{
		clearInterval(this.times);
		this.times=null;
		if(this.menuObj!=null)
			{
			this.menuObj.parentNode.removeChild(this.menuObj);
			this.menuObj=null;
			aiviki.event.removeEventById(this.UIID);
			}
		},
	itemMouseOver:function(e)
		{
		if(this.times!=null)
			{
			clearInterval(this.times);
			this.times=null;
			}
			var obj =e.srcElement;
			obj.className = this.MENUITEMIX;
		},
	itemMouseOut:function(e)
		{
		var obj =e.srcElement;
		obj.className = this.MENUITEMDF;

		},
	mouseout:function(e)
		{
		var o = null;
		if(e.toElement)
			{
			o=e.toElement;
			}else if(e.relatedTarget)
			{
			o=e.relatedTarget;
			}
		if(o.flag!=this.UIID)
			{
			this.times =setInterval(aiviki.bind(this.hide,this),1000,this);
			}
		},
	addItem:function(item,i)
		{
		if(item.ClassId =="aiviki.ui.avkMenuItemTitle" || item.ClassId == "aiviki.ui.avkMenuItem")
			{
			if(typeof i == "undefined")i=0;
			if((this.data.getSize()-1)<i)
				{
				var mg = new aiviki.ui.avkMenuGroup();
				mg.addItem(item);
				this.data.add(mg);
				}else{
				var mg = this.data.get(i);
				mg.addItem(item);
				return true;
				}
			}else{
			return false;
			}
		}

	}
aiviki.ui.avkMenuItem = aiviki.Class.create("aiviki.ui.avkMenuItem");
aiviki.ui.avkMenuItem.prototype = 
	{
	init:function(t,u,tg)
		{
		this.text=t;
		if(typeof u == "undefined")
			this.url="";
			else
			this.url=u;
		if(typeof tg == "undefined")
			this.target=false;
			else
			this.target=tg;
		},
	visible:true
	}


aiviki.ui.avkMenuItemTitle = aiviki.Class.create("aiviki.ui.avkMenuItemTitle");
aiviki.ui.avkMenuItemTitle.prototype = 
	{
	init:function(t)
		{
		this.text=t;
		}
	}

aiviki.ui.avkMenuGroup = aiviki.Class.create("aiviki.ui.avkMenuGroup");
aiviki.ui.avkMenuGroup.prototype = 
	{
	init:function()
		{
		this.data =new aiviki.util.vector(2);
		},
	addItem:function(item)
		{
		this.data.add(item);
		},
	getItem:function(i)
		{
		return this.data.get(i);
		},
	getSize:function()
		{
		return this.data.getSize();
		},
	visible:true
	}

